package com.ht.right.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.ht.right.entity.Role;
import com.ht.right.service.RoleService;
import com.ht.right.util.CommonUtil;
import com.opensymphony.xwork2.ActionSupport;

public class RoleAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private RoleService roleService;
	private Role role;

	public String findAllRoleJson(){
		CommonUtil.toJsonStr(ServletActionContext.getResponse(), roleService.findAllRoleJson());
		return null;
	}
	
	public String findRoleById() {
		role = roleService.findRoleById(role);
		return SUCCESS;
	}
	
	
	public String updateRole() {
		try {
			roleService.updateRole(role);
			String html = "<script>parent.roleLoad();parent.CloseWindow();</script>";
			CommonUtil.toString(ServletActionContext.getResponse(), html);
		} catch (Exception e) {
			CommonUtil
					.toString(ServletActionContext.getResponse(),
							"<script>alert(\"维护失败！\");parent.goCloseDlg(\"oWinDlg\");</script>");
			e.printStackTrace();
		}
		return null;
	}
	
	public void deleteRoles() {
		String roleIds = ServletActionContext.getRequest().getParameter("id");
		roleService.deleteRoles(roleIds);
		String html = "<script>parent.roleLoad();parent.CloseWindow();</script>";
		CommonUtil.toString(ServletActionContext.getResponse(), html);
	}
	
	public String addRole() {
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			String addUserList = request.getParameter("Members");
			System.out.println(addUserList);
			roleService.saveRole(role);
			String html = "<script>parent.roleLoad();parent.CloseWindow();</script>";
			CommonUtil.toString(ServletActionContext.getResponse(), html);
			request.setAttribute("roleId", role.getUuid());
			request.setAttribute("addUserList", addUserList);
		} catch (Exception e) {
			CommonUtil.toString(ServletActionContext.getResponse(),
					"<script>alert(\"维护失败！\");</script>");
			e.printStackTrace();
		}
		return null;
	}
	

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

}
